#include "AES.h"
#include "TestBase.h"

#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif

unsigned long long GetTime()
{
#ifdef WIN32
	LARGE_INTEGER tps, tick, time;
	QueryPerformanceFrequency(&tps);
	QueryPerformanceCounter(&tick);
	time.QuadPart = tick.QuadPart/tps.QuadPart;
	return (unsigned long long)((time.QuadPart * 1000000) + (tick.QuadPart / (tps.QuadPart / 1000000)));
#else
	timeval tv;
	gettimeofday(&tv, NULL);
	return (unsigned long long)((tv.tv_sec * 1000000) + (tv.tv_usec));
#endif
}

TestBase::TestBase()
{
	info.Name = "CryptoSystem Test 3 - AES speed test";
	info.Category = 0x3102;
	info.CategoryName = "CryptoSystem";
	info.File = __FILE__;
}

bool TestBase::Run()
{
#ifdef WIN32
	SetThreadAffinityMask(GetCurrentThread(), 1); // time counters between threads cannot be assured to be in sync, force to 1st core to work around
#endif
	unsigned long long s, e;
	unsigned char key[16] = { // 000102030405060708090a0b0c0d0e0f
		0x00, 0x01, 0x02, 0x03,
		0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0a, 0x0b,
		0x0c, 0x0d, 0x0e, 0x0f
	};
	unsigned char plain[16] = { // 00112233445566778899aabbccddeeff
		0x00, 0x11, 0x22, 0x33,
		0x44, 0x55, 0x66, 0x77,
		0x88, 0x99, 0xaa, 0xbb,
		0xcc, 0xdd, 0xee, 0xff
	};
	unsigned char cipher[16];
	StateAES state;
	s = GetTime();
	AES_Init(&state, key);
	AES_Encrypt(&state, plain);
	AES_Final(&state, cipher);
	e = GetTime();
	std::cout << "** AES speed test **" << std::endl;
	std::cout << "Single block : " << (e - s) << "us" << std::endl;
	return true;
}